package del3;

import java.sql.*;

public class ExecuteQuery {
	
	public void executeQuery(String sql, QueryCallBack callback) {
		Connection con = null;
		Statement statement = null;
		ResultSet rs = null;
		try {
			con = DriverManager.getConnection("jdbc:derby:PersonDB; create=true");
			statement = con.createStatement();
			rs = statement.executeQuery(sql);
			while(rs.next()) {
				callback.processRecord(rs);
			}
		} catch(SQLException sqle) {
			System.out.println("Database problems");
		} finally {
			try {
				if(rs != null) {
					rs.close();
				}
				if(statement != null) {
					statement.close();
				}
				if(con != null) {
					con.close();
				}
			} catch(SQLException sqle) {
				System.out.println("The database connection is still open");
			}
		}
		
	}
}